package com.itheima.task;

import com.itheima.common.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class ImgTask {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;

    @Scheduled(cron = "0 0 1 * * *")
    public void deleteTrashImg(){
        Set<String> diff = redisTemplate.opsForSet().difference("set_oss", "set_db");
        if (diff != null && diff.size() > 0){
            for (String imgName : diff) {
                redisTemplate.opsForSet().remove("set_oss", imgName);
                aliOssUtil.delete(imgName);
                System.out.println("删除垃圾图片：" + imgName);
            }
        }else {
            System.out.println("没有垃圾图片");
        }
    }
}
